package com.future.mayi.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.future.mayi.R;
import com.future.mayi.model.SpecAttrModel;
import com.future.mayi.model.SpecItemsModel;
import com.future.mayi.model.SpecListModel;
import com.future.mayi.model.interfaces.GoodsSelectChangeListener;
import com.future.mayi.model.interfaces.GoodsSelectListener;
import com.future.mayi.widget.FlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GoodsSelectView extends LinearLayout implements View.OnClickListener, GoodsSelectListener {


    private Context mContext;
    private List<SpecAttrModel> list;
    private List<SpecListModel> spec_list;

    private List<GoodsSelectItemView> viewList;

    private int[] selectId;

    private GoodsSelectChangeListener listener;

    public GoodsSelectView(Context context) {
        super(context);
        init(context);
    }

    public GoodsSelectView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public GoodsSelectView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        if (getLayoutParams() == null) {
            setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        } else {
            getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT;
            getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
        }
        setOrientation(VERTICAL);
    }

    public void setData(List<SpecAttrModel> list, List<SpecListModel> spec_list) {
        if (this.list == list) return;
        this.list = list;
        this.spec_list = spec_list;
        selectId = new int[list.size()];
        for (int i = 0; i < selectId.length; i++) {
            selectId[i] = 0;
        }

        int[] skuId;
        for (SpecListModel specListModel : spec_list) {
            skuId = new int[list.size()];
            String[] split = specListModel.getSpec_sku_id().split("_");
            String spec_value = "";
            for (int i = 0; i < split.length; i++) {
                skuId[i] = Integer.valueOf(split[i]);
                for (SpecAttrModel model : list) {
                    if (model.getSpec_items() != null) {
                        for (SpecItemsModel itemsModel : model.getSpec_items()) {
                            if (itemsModel.getItem_id() == skuId[i]) {
                                spec_value = spec_value + itemsModel.getSpec_value() + ",";
                            }
                        }
                    }

                }

            }
            specListModel.setSpec_value(spec_value.length() > 0 ? spec_value.substring(0, spec_value.length() - 1) : spec_value);
            specListModel.setSkuId(skuId);
        }

        removeAllViews();
        viewList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            GoodsSelectItemView view = new GoodsSelectItemView(mContext);
            view.setTag(i);
            addView(view);
            view.setModel(list.get(i), spec_list, selectId);
            view.setListener(this);
            viewList.add(view);
        }
    }

    @Override
    public void onClick(View v) {

    }


    @Override
    public void itemSelect(GoodsSelectItemView selectView, SpecAttrModel specAttrModel, SpecItemsModel specItemsModel) {
        int position = (int) selectView.getTag();
        selectId[position] = specItemsModel.getItem_id();
        for (int i = 0; i < viewList.size(); i++) {
            GoodsSelectItemView view = viewList.get(i);
            if (selectView == view) continue;
            view.setSelect(selectId);
        }

        if (listener == null) return;

        String spec_sku_id = "";
        for (int i = 0; i < selectId.length; i++) {
            if (selectId[i] == 0) return;
            spec_sku_id = spec_sku_id + selectId[i] + "_";
        }

        if (spec_sku_id.length() > 0)
            spec_sku_id = spec_sku_id.substring(0, spec_sku_id.length() - 1);

        for (SpecListModel specListModel : spec_list) {
            if (TextUtils.equals(specListModel.getSpec_sku_id(), spec_sku_id)) {
                listener.onGoodsSelect(this, specListModel);
                return;
            }
        }
        listener.onGoodsSelect(this, null);
    }

    public void setListener(GoodsSelectChangeListener listener) {
        this.listener = listener;
    }
}
